<?php
/**
 * Created by PhpStorm.
 * User: aron
 * Date: 2019/5/8
 * Time: 8:03 PM
 */

include "../../Util/ArrayUtil/ArrayUtil.php";

class Form
{
    private $shape;

    public function __construct($shape = "rect")
    {
        if (empty($shape)) {
            $this->shape = "rect";
        } else {
            $this->shape = $shape;
        }
    }

    public function __toString()
    {
        if (strtolower($this->shape) == "rect") {
            return $this->generateRectForm();
        } else if (strtolower($this->shape) == "circle") {
            return $this->generateCircleForm();
        }
        return "Other Form";
    }

    private function generateRectForm()
    {
        $width = yt_array_value_for_key("width", $_GET);
        $height = yt_array_value_for_key("height", $_GET);

        $formString = "";
        $formString .= "<form action='index.php' method='get'>";
        $formString .= "<input type='hidden' name='shape' value='rect'/>";
        $formString .= "长：";
        $formString .= "<input type='number' name='width' value='". $width ."'/>";
        $formString .= "<br>";
        $formString .= "宽：";
        $formString .= "<input type='number' name='height' value='" . $height . "'/>";
        $formString .= "<br>";
        $formString .= "<input type='submit' value='计算' name='calculate'/>";
        $formString .= "</form>";
        return $formString;
    }

    private function generateCircleForm()
    {
        $radius = yt_array_value_for_key("radius", $_GET);

        $formString = "";
        $formString .= "<form action='index.php' method='get'>";
        $formString .= "<input type='hidden' name='shape' value='circle'/>";
        $formString .= "半径：";
        $formString .= "<input type='number' name='radius' value='". $radius ."'/>";
        $formString .= "<br>";
        $formString .= "<input type='submit' value='计算' name='calculate'/>";
        $formString .= "</form>";
        return $formString;
    }
}